<template>
  <div class="topbar">
    <div class="wrap bar-in">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="login-btn" @click="changeModal(true)" v-if="!isLogined">
          登录
        </li>
        <li class="cart-btn" v-if="isLogined">
          <img src="@/assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WxLogin } from "@/request/api";
export default {
  methods: {
    ...mapMutations({
      changeModal: "loginModal/changeModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  async created() {
    let code = this.$route.query.code;
    if (code) {
      let res = await WxLogin({
        code,
      });
      if (res.code == 0) {
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        this.changeIsLogined(true);
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "扫码登录成功",
          type: "success",
        });
        this.asyncChangeUserInfo();
        return;
      } else if (res.code == 407) {
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: res.message,
          type: "warning",
        });
      } else if (res.code == 400) {
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: res.message,
          type: "warning",
        });
      }
      localStorage.setItem("uuid", res.uuid);
      this.changeModal(true);
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLogined(Boolean(token));
      token ? this.asyncChangeUserInfo() : this.initUserInfo();
    }
  },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         let token = localStorage.getItem("x-auth-token");
  //         this.changeIsLogined(Boolean(token));
  //       }
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  min-width: 1200px;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .bar-in {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .right {
    li {
      float: left;
      margin-left: 20px;
      text-align: center;
      cursor: pointer;
      &.login-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
    }
    img {
      vertical-align: middle;
      width: 23px;
      height: 22px;
      border-radius: 13px;
    }
  }
}
.cart-btn {
  width: 124px;
  height: 40px;
  background: #0a328e;
  text-align: center;
  img {
    width: 20px;
    height: 19px;
    vertical-align: middle;
  }
  span {
    margin: 0 8px;
  }
  strong {
    width: 22px;
    height: 22px;
    line-height: 22px;
    display: inline-block;
    border-radius: 50%;
    background-color: #fd604d;
  }
}
</style>